<?php

namespace app\property\controller;

use app\property\model\ExamineRepairModel;
use app\property\model\PropertyRepairModel;
use app\staff\model\StaffModel;
use think\admin\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Repair extends Controller
{
    /**
     * 当前操作数据库
     * @var string
     */
    private string $table = 'Repair';

    /**
     * 报事报修管理
     * @auth true
     * @menu true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $this->title = '报事报修管理';
        $query = $this->_query(PropertyRepairModel::class);
        // 加载对应数据
        $this->typeOf = $this->request->get('typeOf', 'index');
        if ($this->typeOf == 'index') $query->where(['is_deleted' => 0]);
        elseif ($this->typeOf == 'is_examine') $query->where(['status' => 0, 'is_deleted' => 0]);
        elseif ($this->typeOf == 'is_ing') $query->where(['status' => 2, 'is_deleted' => 0]);
        elseif ($this->typeOf == 'is_repair') $query->where(['status' => 1, 'is_deleted' => 0]);
        elseif ($this->typeOf == 'is_deleted') $query->where(['is_deleted' => 1]);
        $query->like('name,tel')->order('id desc')->page(true, true);
    }

    /**
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function dispatch()
    {
        $id = $this->request->get('id');
        $model1 = new StaffModel();
        $staff = $model1->where(['type' => 5])->select();

        if ($this->request->isPost()) {
            $sid = $this->request->post('sid');
            $model = new PropertyRepairModel();
            $repair = $model->where(['id' => $id])->find();
            if (!$repair) {
                $this->error('操作失败');
            }
            $r1 = $model->where(['id' => $id])->update(['status' => 2]);
            if (!$r1) {
                $this->error('操作失败');
            }
            $staffs = [];
            foreach($staff as $v){
                $staffs[$v['id']] = $v['name'];
            }
            $par = [
                'sid' => $sid,
                'uid' => $repair['uid'],
                'pid' => $id,
                'engineer' => $staffs[$sid],
                'type' => 1
            ];

            $m = new ExamineRepairModel();
            $res = $m->insert($par);
            if (!$res) {
                $this->error('操作失败');
            }
            $this->success('操作成功');
        } else {

            $this->assign('staff', $staff);
            $this->fetch();
        }


    }

    /**
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail()
    {

    }

    /**
     * 表单数据处理
     * @param array $data
     */
    protected function _form_filter(array &$data)
    {

    }

    /**
     * 修改发布状态
     * @auth true
     * @throws DbException
     */
    public function state()
    {
        $this->_applyFormToken();
        $this->_save(PropertyRepairModel::class, $this->_vali([
            'status.in:-1,1' => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * @throws DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        $this->_save(PropertyRepairModel::class, $this->_vali([
            'is_deleted.in:0,1' => '状态值范围异常！',
            'is_deleted.require' => '状态值不能为空！',
        ]));
    }
}